package 二叉搜索数;


import com.sun.source.tree.Tree;

/**
 * https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/?envType=study-plan-v2&envId=top-100-liked
 */
public class _108_将有序数组转换为二叉搜索树 {

    public TreeNode sortedArrayToBST(int[] nums) {
        return buildTreeNode(nums, 0, nums.length-1);
    }

    private TreeNode buildTreeNode(int[] nums, int left, int right) {
        if (left > right) return null;
        int middle = (left + right) / 2;
        TreeNode root = new TreeNode(nums[middle]);
        root.left = buildTreeNode(nums, left, middle - 1);
        root.right = buildTreeNode(nums, middle + 1, right);
        return root;
    }
}
